﻿using ConfigInfoWebPlatform.API.DBCommon;
using ConfigInfoWebPlatform.Model;
using ConfigInfoWebPlatform.Model.Model;
using ConfigInfoWebPlatform.Nlog;
using FileSecretAssemblyStandard;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.Controllers
{
    /// <summary>
    /// 公共模块控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CommonCfgFileController : ControllerBase
    {
        readonly LogHelper _log;
        readonly string configRootPath;
        readonly string initDB;
        readonly InterfaceFileSecret FileSecret;
        /// <summary>
        /// 公共模块控制器
        /// </summary>
        /// <param name="logHelper"></param>
        /// <param name="configuration"></param>
        /// <param name="fileSecret"></param>
        public CommonCfgFileController(LogHelper logHelper, IConfiguration configuration, InterfaceFileSecret fileSecret)
        {
            _log = logHelper;
            configRootPath = configuration["AppSetting:configPath"].ToString();
            initDB = configuration["AppSetting:initDB"].ToString();
            FileSecret = fileSecret;
        }
        /// <summary>
        /// 查看文件内容
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <returns></returns>
        [HttpGet("detail")]
        public ActionResult Detail(string fileName)
        {
            string fileContent = "";
            try
            {
                string filenametemp = Path.Combine(configRootPath, fileName);
                fileContent = FileSecret.DecryptFileToContent(filenametemp);
                return new JsonResult(fileContent);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }
        /// <summary>
        /// Index
        /// </summary>
        /// <param name="dir">首页</param>
        /// <param name="page">首页</param>
        /// <param name="limit">首页</param>
        /// <returns></returns>
        // GET: Manager/CaseElementCfgFile
        [HttpGet("index")]
        public JsonResult Index(string dir, int page, int limit)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            // 获取流程配置的文件结构
            DAL_ConfigFile configFile = new DAL_ConfigFile();
            List<ConfigFileInfoModel> caseElementCfgFileInfos = configFile.GetConfigFiles(dir);
            // 获取页面的数据
            if (caseElementCfgFileInfos != null && caseElementCfgFileInfos.Count > 0)
            {
                caseElementCfgFileInfos = caseElementCfgFileInfos.Skip((page - 1) * limit).Take(limit).ToList();
            }
            return new JsonResult(new { code = 0, msg = "成功", count = caseElementCfgFileInfos.Count(), data = caseElementCfgFileInfos });
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        [HttpGet("downloadfile")]
        public ActionResult DownLoadFile(string fileName)
        {
            Result re = new Result();
            if (!string.IsNullOrEmpty(fileName))
            {
                re.code = "1";
                string filenameTemp = Path.Combine(configRootPath, fileName);
                string clientFileName = Path.GetFileName(fileName);
                string fileContent = FileSecret.DecryptFileToContent(filenameTemp);
                byte[] bytes = Encoding.UTF8.GetBytes(fileContent);
                //二进制流数据（如常见的文件下载）
                return File(bytes, "application/octet-stream", clientFileName);
            }
            else
            {
                re.code = "0";
                re.message = "下载失败";
                return BadRequest(re);
            }
        }

    }
}
